package com.example.com.daervideo.activitys.users;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.com.daervideo.R;
import com.example.com.daervideo.activitys.BaseListActivity;
import com.example.com.daervideo.databinding.ActivityFansBinding;
import com.example.com.daervideo.databinding.ItemFansBinding;
import com.example.com.daervideo.entity.BaseListEntity;
import com.example.com.daervideo.entity.SimpleUser;
import com.example.com.daervideo.initerface.ApiService;
import com.example.com.daervideo.utils.RetrofitClient;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

import com.example.com.daervideo.BR;

import java.util.ArrayList;

/**
 * Created by i7-1 on 2018/12/6.
 * 我的关注和我的粉丝
 */

public class FansActivity extends BaseListActivity<ActivityFansBinding, BaseViewModel> {

    private int mIndex;//1：我的关注 2:我的粉丝。
    private TextView empty;
    private ApiService apiService;
    private RecyclerView recyclerView;


    @Override
    public int initContentView() {
        return R.layout.activity_fans;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public BaseViewModel initViewModel() {
        return new BaseViewModel(this);
    }

    @Override
    protected TwinklingRefreshLayout getRefreshLayout() {
        return getBinding().myInclude.refreshLayout;
    }

    @Override
    public void initData() {
        super.initData();
        empty = findViewById(R.id.rv_empty);

        apiService = RetrofitClient.getInstance().create(ApiService.class);
        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new AAdapter());

        TextView view = (TextView) findViewById(R.id.my_tool_bar).findViewById(R.id.title);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (bundle != null) {
            mIndex = bundle.getInt("index");
            if (mIndex == 1) {
                view.setText("我的关注");
            } else {
                view.setText("我的粉丝");
            }
        }

        getBinding().myInclude.refreshLayout.startRefresh();

    }


    private class AAdapter extends RecyclerView.Adapter<AHolder> {
        @Override
        public AHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ItemFansBinding binding = DataBindingUtil.inflate(LayoutInflater.from(FansActivity.this), R.layout.item_fans, parent, false);
            return new AHolder(binding);
        }

        @Override
        public void onBindViewHolder(AHolder holder, final int position) {

            final SimpleUser simpleUser = new SimpleUser();
            holder.getBinding().setVariable(BR.entity, simpleUser);
            holder.getBinding().btnAttention.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    action_attetion(1, simpleUser, position);
                }
            });
            holder.getBinding().btnDisattention.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    action_attetion(2, simpleUser, position);
                }
            });
            // 刷新
            holder.getBinding().executePendingBindings();
        }

        @Override
        public int getItemCount() {
            return 0;
        }
    }


    private class AHolder extends RecyclerView.ViewHolder {
        private final ItemFansBinding binding;

        public AHolder(ItemFansBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ItemFansBinding getBinding() {
            return binding;
        }
    }


    public void getNetData(final int page, int pageSize) {

        Observable<BaseResponse<BaseListEntity>> Observable;
        if (mIndex == 1) {
            Observable = apiService.myAttention(pageSize, page);
        } else {
            Observable = apiService.myFans(pageSize, page);
        }
        Observable.compose(RxUtils.bindToLifecycle(this)) //请求与View周期同步
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .subscribe(new Consumer<BaseResponse<BaseListEntity>>() {
                    @Override
                    public void accept(BaseResponse<BaseListEntity> response) throws Exception {

                        if (page == 1) {
                            getRefreshLayout().finishRefreshing();
                        } else {
                            getRefreshLayout().finishLoadmore();
                        }
                        if (response.getCode() == 0) {
                            recyclerView.getAdapter().notifyDataSetChanged();
                            getBinding().myInclude.setSize(0);
                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }, new Consumer<ResponseThrowable>() {
                    @Override
                    public void accept(ResponseThrowable throwable) throws Exception {
                        if (page == 1) {
                            getRefreshLayout().finishRefreshing();
                        } else {
                            getRefreshLayout().finishLoadmore();
                        }

                        ToastUtils.showShort(throwable.message);
                        throwable.printStackTrace();
                    }
                });
    }

    /**
     * @param i          1：关注 ，2：取消关注
     * @param simpleUser
     * @param position
     */
    private void action_attetion(int i, SimpleUser simpleUser, int position) {

        Observable<BaseResponse<BaseListEntity>> Observable;

        if (mIndex == 1) {
            Observable = apiService.action_attetion(simpleUser.getUserId());
        } else {
            Observable = apiService.action_disAttetion(simpleUser.getUserId());
        }
        Observable.compose(RxUtils.bindToLifecycle(this)) //请求与View周期同步
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .subscribe(new Consumer<BaseResponse<BaseListEntity>>() {
                    @Override
                    public void accept(BaseResponse<BaseListEntity> response) throws Exception {

                        if (response.getCode() == 0) {

                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }, new Consumer<ResponseThrowable>() {
                    @Override
                    public void accept(ResponseThrowable throwable) throws Exception {

                        ToastUtils.showShort(throwable.message);
                        throwable.printStackTrace();
                    }
                });

    }
}
